package com.ruoyi.flow.auth.security.sso;

import com.ruoyi.flow.auth.security.sso.SsoAuthenticator;
import com.ruoyi.flow.auth.security.sso.SsoUsernamePassword;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.web.util.matcher.RequestMatcher;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author wtian
 * @version 1.0
 * @since 2018/9/12 14:54
 */
public interface SsoUsernamePasswordProvider {
    /**
     * 根据参数中的相关信息获取用户名密码
     *
     * @param parameters
     * @return
     */
    SsoUsernamePassword getUsernameAndPassword(Map<String, String[]> parameters);

    /**
     * 判断接收的请求是否是该provider需要处理的请求
     *
     * @return
     */
    boolean matchRequest(HttpServletRequest request);

    /**
     * 执行认证校验
     *
     * @param provider
     * @param parameters
     * @return
     */
    default SecurityContext authenticate(AuthenticationProvider provider, Map<String, String[]> parameters) {
        SsoAuthenticator authenticator = new SsoAuthenticator(this);
        return authenticator.authenticate(provider, parameters);
    }

    /**
     * 获取该Provider用于处理哪种请求信息
     *
     * @return
     * @see RequestMatcher
     */
    default RequestMatcher matcher() {
        return new RequestMatcher() {
            @Override
            public boolean matches(HttpServletRequest httpServletRequest) {
                return matchRequest(httpServletRequest);
            }
        };
    }
}
